<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Salmon_Follow extends Salmon_Abstract  {
    
    public function createFollowAtomAndSendDoEverythingThisIsTemporarTest(Minime_User $who, Discovery_Uri $discoverer) {
        // Let's create the needed elements.
        $id = new Atom_Entry_Id("mimime:" . time());
        $title = new Atom_Entry_Title($who->getName() . " is now following " . $discoverer->getName());
        $content = new Atom_Entry_Content('<a href="' . html_entity_decode($who->getUri()) . '">' .  $who->getName() . '</a> is now following <a href="' . $discoverer->getWebfingerProfilePageUri() . '">' . $discoverer->getName() . '</a>.', 
                                          Atom_Entry_Content::TYPE_HTML);
        
        $author = new Atom_Entry_Author();
        //$author->addChildElement(new Atom_Entry_Author_Uri($who->getUri()));
        // This is changed since did not work with identi.ca, could it be some short
        // of a cache problem?
        $author->addChildElement(new Atom_Entry_Author_Uri("acct:" . $who->getJid()));
        $author->addChildElement(new Atom_Entry_Author_Name($who->getName()));
        $author->addChildElement(new Atom_Entry_Author_Link_Photo($who->getLogo(), "image/png"));
        $author->addChildElement(new Atom_Entry_Author_Link_AlternateHtml(Minime_HrefRenderer::getLdrrProfileHref($who->getNode(), Minime::$conf), "image/png"));
        
        // Let's create the activity actor
        $as_actor = new Activitystream_Actor();
        $as_actor->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_PERSON));
        
        $as_actor->addChildElement(new Atom_Entry_Id($who->getUri()));
        $as_actor->addChildElement(new Atom_Entry_Title($who->getName()));
        $as_actor->addChildElement(new Atom_Entry_Link(Atom_Entry_Link::REL_ALTERNATE, 
                                                       Atom_Entry_Link::TYPE_TEXTHTML, 
                                                       $who->getUri()));
        $as_actor->addChildElement(new Atom_Entry_Link_Avatar($who->getLogo()));
        $as_actor->addChildElement(new Atom_Entry_Link_Photo($who->getLogo()));
        
        // And let's create the activity verb
        $as_verb = new Activitystream_Verb(Activitystream_Verb::VERB_FOLLOW);

        
        $as_object = new Activitystream_Object();
        if($discoverer->getAtomFeed()->getActivityStreamSubject() &&
           $discoverer->getAtomFeed()->getActivityStreamSubject()->getObjectType()->getObjectType() == Activitystream_ObjectType::TYPE_GROUP) {
            $as_object->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_GROUP));   
        } else {
            $as_object->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_PERSON));
        }
        $as_object->addChildElement(new Atom_Entry_Title($discoverer->getName()));
        $as_object->addChildElement(new Atom_Entry_Id($discoverer->getFeedsAuthorUri()));
        $as_object->addChildElement(new Atom_Entry_Author_Link_AlternateHtml($discoverer->getWebfingerProfilePageUri()));
        $as_object->addChildElement(new Atom_Entry_Author_Link_Photo($discoverer->getLogo(), "image/png"));
        // Finally let's create the Atom Entry itself
        $atom = new Atom_Entry();
        
        $atom->addChildElement($id);
        $atom->addChildElement($title);
        $atom->addChildElement($content);
        $atom->addChildElement($author);
        $atom->addChildElement($as_actor);
        $atom->addChildElement($as_verb);
        $atom->addChildElement($as_object);
        
        // Let's add needed new namespaces.
        $atom->addNamespace(ActivityStream::AS_NS_PREFIX, Activitystream::AS_NS);
        
        $signer = new Magic_Signature_Sign($who->getEncodedPrivateKey());
        
        $encodedPayload = Magic::base64_url_encode($atom->asXML());
        
        $signature = $signer->sign($encodedPayload);
        
        $magicEnvelope = Magic_Envelope::build($encodedPayload, $signature);
        $envelope = $magicEnvelope->asXML();
        
        echo "\nReady to send the following atom (as decoded):\n";
        echo $atom->asXML();
        
        return $this->postSalmon($discoverer->getSalmonUri(), $envelope);
        
    }
    
}